<?php
if(!defined('IN_WOGU')) {
    exit('Access Denied');
}

Loader::loadUserClass('MemberController', false, 'admin');

class Admin_CatController extends MemberController
{
    protected $_cat;
    
    protected function _initMember() {
        $this->_cat = Loader::loadUserModel('CatModel', true, 'admin');
    }
    
    public function manCatAction() {
        if($_POST['doadd']) {
            $id = intval($_POST['id']);
            $parentid = intval($_POST['parentid']);
            $catname = new_htmlspecialchars(trim($_POST['cat_name']));
            $data = array('cat_name' => $catname);
            if($id) {
                $this->_cat->updateCat($data, array('id' => $id));
                showmessage($this->_lang['edit_success'], HTTP_REFERER);
            }
            
            $data['parent_id'] = $parentid;
            $this->_cat->addCat($data);
            showmessage($this->_lang['add_success'], ADMIN_NAME.'?m=admin&c=cat&a=man_cat&parentid='.$parentid);
        }
        
        if($_GET['op'] == 'stop') {
            $id = intval($_GET['id']);
            $flag = $_GET['flag'] == 1 ? 1 : 0;
            $this->_cat->updateCat(array('flag' => $flag), array('id' => $id));
            showmessage($this->_lang['success'], HTTP_REFERER);
        } elseif($_GET['op'] == 'addcat') {
            $id = intval($_GET['id']);
            $parentid = intval($_GET['parentid']);
            if($id) {
                $cat = $this->_cat->getCat(array('id' => $id));
            }
            
            $this->_view->assign(array(
                'cat' => $cat,
                'parentid' => $parentid
            ));
            $this->_view->display('cat_addcat');
        } elseif($_GET['op'] == 'delete') {
            $id = intval($_GET['id']);
            $this->_cat->deleteCat($id);
            showmessage($this->_lang['del_success'], HTTP_REFERER);
        } else {
            $page = max(1, intval($_GET['page']));
            $parentid = intval($_GET['parentid']);
            $where = array('parent_id' => $parentid);
            $cats = $this->_cat->listCats($where, $page, 50);

            $this->_view->assign(array(
                'cats' => $cats,
                'parentid' => $parentid
            ));
            
            $this->_view->display('cat_mancat');
        }
    }
    
    public function updateCacheAction() {
        $cats = $this->_cat->getCats();
        foreach($cats as $k => $v) {
            if($this->getFlag($k, $cats)) {
                $toppid = $this->getTopParent($k, $cats);
                $topcats[$toppid][$v['parent_id']][$k] = $v['cat_name'];
            }
        }

        foreach($topcats as $toppid => $data) {
            unset($data[0]);
            ob_start();
            $len1 = count($data);
            $index1 = 0;
            echo '{';
            foreach($data as $k => $v) {
                $len2 = count($v);
                $index2 = 0;
                echo '"'.$k.'":{';
                foreach($v as $k1 => $v1) {
                    echo '"'.$k1.'":"'.$v1.'"'.($index2 == $len2 - 1 ? '' : ',');
                    $index2++;
                }
                echo '}'.($index1 == $len1 - 1 ? '' : ',');
                $index1++;
            }
            echo '}';
            $data = ob_get_clean();
            $this->_view->assign(array(
                'data' => $data,
                'toppid' => $toppid
            ));
            $js = $this->_view->render('cat_cache');
            file_put_contents(WOGU_ROOT . 'static/js/cache/cat_'.$toppid.'.js', $js);
        }

        showmessage($this->_lang['success']);
    }
    
    protected function getFlag($id, $cats) {
        do {
            if($cats[$id]['flag'] == 1) {
                return false;
            }
            $id = $cats[$id]['parent_id'];
        } while($cats[$id]);
        
        return true;
    }
    
    protected function getTopParent($id, $cats) {
        while(true) {
            if($cats[$id]['parent_id'] == 0) {
                break;
            }
            
            $id = $cats[$id]['parent_id'];
        }

        return $id;
    }
}